const getColor = (startColor, endColor, step) => {
  startColor = getColorArr(startColor);
  endColor = getColorArr(endColor);
  let stepR = (endColor[0] - startColor[0]) / step; // 总差值
  let stepG = (endColor[1] - startColor[1]) / step;
  let stepB = (endColor[2] - startColor[2]) / step;
  let colorArr = [];
  for (let i = 0; i < step; i++) {
    let color = `rgb(${stepR * i + startColor[0]},${stepG * i + startColor[1]},${
      stepB * i + startColor[2]
    })`;
    colorArr.push(color);
  }
  return colorArr;
};

const getColorArr = (value) => {
  if (isRgba(value)) {
    return changeRgba(value);
  } else if (isHex(value)) {
    return changeHex(value);
  }
};

const isRgba = (value) => {
  return /^rgba?/.test(value);
};

const isHex = (value) => {
  return /^#/.test(value);
};

const hexToRgb = (hex) => {
  if (hex.length == 4) {
    let color = `rgb(${parseInt('0x' + hex.slice(1, 2).repeat(2))},${parseInt(
      '0x' + hex.slice(2, 3).repeat(2)
    )},${parseInt('0x' + hex.slice(3, 4).repeat(2))})`;
    return color;
  } else if (hex.length == 7) {
    let color = `rgb(${parseInt('0x' + hex.slice(1, 3))},${parseInt(
      '0x' + hex.slice(3, 5)
    )},${parseInt('0x' + hex.slice(5, 7))})`;
    return color;
  } else alert('颜色错误');
};

const changeHex = (hex) => {
  if (hex.length == 4)
    return [
      parseInt('0x' + hex.slice(1, 2).repeat(2)),
      parseInt('0x' + hex.slice(2, 3).repeat(2)),
      parseInt('0x' + hex.slice(3, 4).repeat(2)),
    ];
  else if (hex.length == 7)
    return [
      parseInt('0x' + hex.slice(1, 3)),
      parseInt('0x' + hex.slice(3, 5)),
      parseInt('0x' + hex.slice(5, 7)),
    ];
  else alert('颜色错误');
};

const changeRgba = (color) => {
  let values = color
    .replace(/rgba?\(/, '')
    .replace(/\)/, '')
    .replace(/[\s+]/g, '')
    .split(',');
  let a = parseFloat(values[3] || 1),
    r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
    g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
    b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
  let newColor =
    '#' +
    ('0' + r.toString(16)).slice(-2) +
    ('0' + g.toString(16)).slice(-2) +
    ('0' + b.toString(16)).slice(-2);
  return changeHex(newColor);
};

const rgbaToHex = (color) => {
  let values = color
    .replace(/rgba?\(/, '')
    .replace(/\)/, '')
    .replace(/[\s+]/g, '')
    .split(',');
  let a = parseFloat(values[3] || 1),
    r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
    g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
    b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
  let newColor =
    '#' +
    ('0' + r.toString(16)).slice(-2) +
    ('0' + g.toString(16)).slice(-2) +
    ('0' + b.toString(16)).slice(-2);
  newColor = values[3] ? newColor + ('0' + Math.round(a * 255).toString(16)).slice(-2) : newColor;
  return newColor;
};

export { getColor, changeHex, changeRgba, hexToRgb, rgbaToHex };
